package com.llc.mall.rbac.interfaces;

import com.llc.mall.rbac.application.command.MenuAppService;
import com.llc.mall.rbac.application.query.MenuQueryService;
import com.mall4j.cloud.api.auth.bo.UserInfoInTokenBO;
import com.llc.mall.common.core.exception.Mall4cloudException;
import com.llc.mall.common.core.response.ServerResponseEntity;
import com.llc.mall.common.security.AuthUserContext;
import com.llc.mall.rbac.application.old.dto.MenuDTO;
import com.llc.mall.rbac.infrastructure.persist.po.MenuPo;
import com.llc.mall.rbac.interfaces.vo.MenuSimpleVo;
import com.llc.mall.rbac.interfaces.vo.MenuVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.llc.mall.common.core.util.BeanUtil;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.List;
import java.util.Objects;

/**
 * @author
 * @date 2020/08/06
 */
@RequestMapping(value = "/menu")
@RestController
@Tag(name = "菜单接口")
public class MenuController {

    private final MenuQueryService menuQueryService;
	private final MenuAppService menuAppService;

    public MenuController(MenuQueryService menuQueryService, MenuAppService menuAppService) {
        this.menuQueryService = menuQueryService;
		this.menuAppService = menuAppService;
    }

//
//	@GetMapping(value = "/route")
//	@Operation(summary = "路由菜单" , description = "获取当前登陆用户可用的路由菜单列表")
//	public ServerResponseEntity<List<RouteVO>> route(Integer sysType) {
//		menuQueryService.routes(sysType);
//
//		sysType = Objects.isNull(sysType) ? AuthUserContext.get().getSysType(): sysType;
//		List<MenuPo> menus = menuService.listBySysType(sysType);
//
//		List<RouteVO> routes = new ArrayList<>(menus.size());
//
//		for (MenuPo menu : menus) {
////			RouteVO route = new RouteVO();
////			route.setAlwaysShow(BooleanUtil.isTrue(menu.getAlwaysShow()));
////			route.setComponent(menu.getComponent());
////			route.setHidden(BooleanUtil.isTrue(menu.getHidden()));
////			route.setName(menu.getName());
////			route.setPath(menu.getPath());
////			route.setRedirect(menu.getRedirect());
////			route.setId(menu.getMenuId());
////			route.setParentId(menu.getParentId());
////			route.setSeq(menu.getSeq());
////
////			RouteMetaVO meta = new RouteMetaVO();
////			meta.setActiveMenu(menu.getActiveMenu());
////			meta.setAffix(BooleanUtil.isTrue(menu.getAffix()));
////			meta.setBreadcrumb(BooleanUtil.isTrue(menu.getBreadcrumb()));
////			meta.setIcon(menu.getIcon());
////			meta.setNoCache(BooleanUtil.isTrue(menu.getNoCache()));
////			meta.setTitle(menu.getTitle());
////			// 对于前端来说角色就是权限
////			meta.setRoles(Collections.singletonList(menu.getPermission()));
//
////			route.setMeta(meta);
////			routes.add(route);
//		}
//		return ServerResponseEntity.success(routes);
//	}

    @GetMapping
    @Operation(summary = "获取菜单管理", description = "根据menuId获取菜单管理")
    public ServerResponseEntity<MenuVo> getByMenuId(@RequestParam Long menuId) {
        MenuVo result = menuQueryService.getMenuById(menuId);
        return ServerResponseEntity.success(result);
    }


    @GetMapping(value = "/list_with_permissions")
    @Operation(summary = "菜单列表和按钮列表", description = "根据系统类型获取该系统的菜单列表 + 菜单下的权限列表")
    public ServerResponseEntity<List<MenuSimpleVo>> listWithPermissions() {
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        List<MenuSimpleVo> result = menuQueryService.simpleMenus(userInfoInTokenBO.getSysType());
        return ServerResponseEntity.success(result);
    }

    @GetMapping(value = "/list_menu_ids")
    @Operation(summary = "获取当前用户可见的菜单ids", description = "获取当前用户可见的菜单id")
    public ServerResponseEntity<List<Long>> listMenuIds() {
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        List<Long> menuList = menuQueryService.listMenuIds(userInfoInTokenBO.getUserId());
        return ServerResponseEntity.success(menuList);
    }


    @PostMapping
    @Operation(summary = "保存菜单管理", description = "保存菜单管理")
    public ServerResponseEntity<Void> save(@Valid @RequestBody MenuDTO menuDTO) {
        MenuPo menu = checkAndGenerate(menuDTO);
//		menu.setMenuId(null);
//        menuService.save(menu);
		menuAppService.save();
        return ServerResponseEntity.success();
    }


    @PutMapping
    @Operation(summary = "更新菜单管理", description = "更新菜单管理")
    public ServerResponseEntity<Void> update(@Valid @RequestBody MenuDTO menuDTO) {
        MenuPo menu = checkAndGenerate(menuDTO);
//        menuService.update(menu);
		menuAppService.update();
        return ServerResponseEntity.success();
    }

    private MenuPo checkAndGenerate(@RequestBody @Valid MenuDTO menuDTO) {
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        if (!Objects.equals(userInfoInTokenBO.getTenantId(), 0L)) {
            throw new Mall4cloudException("无权限操作！");
        }
        MenuPo menu = BeanUtil.map(menuDTO, MenuPo.class);
//		menu.setBizType(menuDTO.getSysType());
        if (Objects.isNull(menuDTO.getSysType())) {
//			menu.setBizType(AuthUserContext.get().getSysType());
        }
        return menu;
    }

    @DeleteMapping
    @Operation(summary = "删除菜单管理", description = "根据菜单管理id删除菜单管理")
    public ServerResponseEntity<Void> delete(@RequestParam("menuId") Long menuId, @RequestParam("sysType") Integer sysType) {
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        if (!Objects.equals(userInfoInTokenBO.getTenantId(), 0L)) {
            throw new Mall4cloudException("无权限操作！");
        }
        sysType = Objects.isNull(sysType) ? userInfoInTokenBO.getSysType() : sysType;
//        menuService.deleteById(menuId, sysType);
		menuAppService.delete();
        return ServerResponseEntity.success();
    }
}
